# Copyright (c) 2017 Presto Labs Pte. Ltd.
# Author: jaewon

import random


def random_split(target_list, num_group):
  n = int(len(target_list) / num_group)
  left = len(target_list) - (n * num_group)

  target_list = list(target_list)
  random.shuffle(target_list)

  groups = []
  idx = 0
  for i in range(num_group):
    group_len = n
    if left > 0:
      group_len = n + 1
      left -= 1
    groups.append(sorted(target_list[idx:idx + group_len]))
    idx += group_len

  assert idx == len(target_list)
  return groups
